package com.example.administrator.demoa;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ListView stulistview;
    private Button btnadd;
    private  StuAdapter adapter;
    private ArrayList<Student> students=new ArrayList<Student>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        stulistview=findViewById(R.id.stu_list);
        btnadd=findViewById(R.id.add_stu);

        adapter=new StuAdapter(MainActivity.this,students);
        stulistview.setAdapter(adapter);
        readDataFromDB();
        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,addActivity.class);
                startActivityForResult(intent,1001);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==2001){
            String stuno=data.getStringExtra("stuno");
            String name=data.getStringExtra("name");
            int age=data.getIntExtra("age",0);
            Student s=new Student(stuno,name,age);
            addStudentToDB(s);
        }
    }

    private void addStudentToDB(Student s) {
        String path=getFilesDir().getAbsolutePath()+File.separator+"stu.db";
        SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(path,null);
        ContentValues contentValues=new ContentValues();
        contentValues.put("stuno",s.getStuno());
        contentValues.put("name",s.getName());
        contentValues.put("age",s.getAge());
        db.insert("student",null,contentValues);
        db.close();
        readDataFromDB();
    }

    private void readDataFromDB(){
        students.clear();
        String path=getFilesDir().getAbsolutePath()+ File.separator+"stu.db";
        SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(path,null);
        String  sql="create table if not exists student(stuno varchar(30),name varchar(30),age int)";
        db.execSQL(sql);
        Cursor cursor= db.query("student",null,null,null,null,null,null);
        if(cursor!=null&&cursor.getCount()>0){
        for(cursor.moveToFirst();!cursor.isLast();cursor.moveToNext()){
            String stuno =cursor.getString(0);
            String name=cursor.getString(1);
            int age=cursor.getInt(0);
            Student s=new Student(stuno,name,age);
            students.add(s);
        }
        }
        cursor.close();
        db.close();
        adapter.notifyDataSetChanged();

    }



}
